package com.example.chatgptapplication.exception;


import com.example.chatgptapplication.enums.ResponseStatus;
import com.example.chatgptapplication.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(IOException.class)
    public ResponseData handleIOException(IOException e){
        log.error(e.toString());
        return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(),ResponseStatus.INTERNAL_SERVER_ERROR.getMessage(),null);
    }


    @ExceptionHandler(NullPointerException.class)
    public ResponseData handleNullPointException(NullPointerException e){
        e.printStackTrace();
        log.error("空指针:" + e.toString());
        return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(),ResponseStatus.INTERNAL_SERVER_ERROR.getMessage(), null);
    }


    @ExceptionHandler(MyException.class)
    public ResponseData handleMyException(MyException e){
        log.error(e.toString());
        return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(),ResponseStatus.INTERNAL_SERVER_ERROR.getMessage(),null);
    }


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResponseData hanleDuplicateEntryException(SQLIntegrityConstraintViolationException e){
        log.error(e.toString());
        return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(),"数据库操作失败!",null);
    }


}
